package com.dabai.simpleconnection.utils;

import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.dabai.dbutils.dialog.MdcDialog;
import com.dabai.simpleconnection.R;
import com.dabai.uitools.utils.ViewUtils;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.textfield.TextInputLayout;

import java.util.Objects;

/**
 * Description : Other
 *
 * @author BAI
 */
public class DialogUtils {


    public interface OnInputDialog2ButtonListener {
        /**
         * 确认
         *
         * @param dialog  对话框
         * @param content 内容
         */
        void confirm(DialogInterface dialog, String content);

        /**
         * 取消
         */
        void cancel();

    }

    /**
     * 显示输入dialog2
     * 显示输入对话框
     * 确认时,请手动dismiss
     *
     * @param context                      上下文
     * @param title                        标题
     * @param hint                         提示
     * @param defaultText                  默认文本
     * @param onInputDialog2ButtonListener 在输入dialog2按钮侦听器
     * @return {@link AlertDialog}
     */
    public static AlertDialog showInputDialog2(@NonNull Context context,@NonNull String title, @NonNull String defaultText,@NonNull String hint, OnInputDialog2ButtonListener onInputDialog2ButtonListener) {

        View dialogView = View.inflate(context, R.layout.dialog_input2, null);
        TextView textView = dialogView.findViewById(R.id.titleView);
        TextInputLayout til1 = dialogView.findViewById(R.id.til1);
        til1.setHint(hint);

        if (!defaultText.isEmpty()) {
            Objects.requireNonNull(til1.getEditText()).setText(defaultText);
        }

        textView.setText(title);

        AlertDialog alertDialog = new MaterialAlertDialogBuilder(context)
                .setView(dialogView)
                .setNeutralButton("取消", (dialog, which) -> onInputDialog2ButtonListener.cancel())
                .setPositiveButton("确认", null).show();

        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(view -> {
            String content = Objects.requireNonNull(til1.getEditText()).getText().toString();
            onInputDialog2ButtonListener.confirm(alertDialog, content);
        });

        return alertDialog;
    }


    public interface OnInputDialogButtonListener {
        /**
         * 确认
         *
         * @param dialog 对话框
         * @param text1  text1
         * @param text2  text2
         */
        void confirm(AlertDialog dialog, String text1,String text2);

        /**
         * 取消
         */
        void cancel();

    }

    /**
     * 显示输入对话框
     * 确认时,请手动dismiss
     *
     * @param context                     上下文
     * @param title                       标题
     * @param onInputDialogButtonListener 输入对话框按钮侦听器
     * @return {@link AlertDialog}
     */
    public static AlertDialog showInputDialog(@NonNull Context context, @NonNull String title,String def1,String def2, OnInputDialogButtonListener onInputDialogButtonListener) {

        View dialogView = View.inflate(context, R.layout.dialog_input, null);
        TextView textView = dialogView.findViewById(R.id.titleView);
        TextInputLayout til1 = dialogView.findViewById(R.id.til1);
        TextInputLayout til2 = dialogView.findViewById(R.id.til2);

        textView.setText(title);

        til1.getEditText().setText(def1);
        til2.getEditText().setText(def2);

        AlertDialog alertDialog = new MaterialAlertDialogBuilder(context)
                .setView(dialogView)
                .setNeutralButton("取消", (dialog, which) -> onInputDialogButtonListener.cancel())
                .setPositiveButton("确认", null).show();

        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(view -> {
            String content1 = ViewUtils.getTextInputLayoutText(til1);
            String content2 = ViewUtils.getTextInputLayoutText(til2);
            onInputDialogButtonListener.confirm(alertDialog, content1,content2);
        });

        return alertDialog;
    }


}
